package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.adapter.personage.InComeAdapter;
import com.wenyu.wenyuyuedu.bean.personage.IncomeDetailBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.InComeDetailServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*收支明细*/
public class MoueyDetail extends AppCompatActivity implements View.OnClickListener {
    @InjectView(R.id.money_lv)
    ListView moneyLv;
    @InjectView(R.id.tv_mingxi)
    TextView tvMingxi;
    @InjectView(R.id.playtour_left)
    ImageView playtourLeft;
    private Activity activity;
    private InComeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mouey_detail);
        ButterKnife.inject(this);
        this.activity = MoueyDetail.this;
        StatusBarUtil.setStatusBar(activity, R.color.white);
        initView();
        requestData();
    }

    /*收支明细展示*/
    private void requestData() {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        InComeDetailServer inComeDetailServer = ApiWrapper.getInstance().create(InComeDetailServer.class);
        Call<IncomeDetailBean> incomeDatail = inComeDetailServer.getIncomeDatail(token);
        incomeDatail.enqueue(new Callback<IncomeDetailBean>() {
            @Override
            public void onResponse(Call<IncomeDetailBean> call, Response<IncomeDetailBean> response) {
                IncomeDetailBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        List<IncomeDetailBean.InfoBean> info = body.getInfo();
                        if (info != null) {
                            adapter.addAll(info);
                        }
                    } else if (code == 0) {
                        showLoginDoalog();
                    } else if (code == 5800) {
                        //收支明细
                        tvMingxi.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFailure(Call<IncomeDetailBean> call, Throwable t) {
                MyToast.showToast(activity, "网络异常，请稍后重试！", Toast.LENGTH_SHORT);
            }
        });
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(activity, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }

    /*初始化控件*/
    private void initView() {
        playtourLeft.setOnClickListener(this);
        adapter = new InComeAdapter(activity);
        moneyLv.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*返回*/
            case R.id.playtour_left:
                finish();
                break;
        }
    }
}
